namespace Leey.Net.eTerm
{
    using System;
    using System.Xml;

    public class NewPara
    {
        private XmlDocument doc;

        public NewPara()
        {
            this.doc = null;
            this.doc = new XmlDocument();
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
            xml = xml + "<eg>" + "</eg>";
            this.doc.LoadXml(xml);
        }

        public NewPara(string p_strXML)
        {
            this.doc = null;
            this.doc = new XmlDocument();
            try
            {
                this.doc.LoadXml(p_strXML);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        public XmlNode AddPara(string p_strParaName, string p_strParaVal)
        {
            XmlNode documentElement = this.doc.DocumentElement;
            XmlNode newChild = this.doc.CreateNode(XmlNodeType.Element, p_strParaName, "");
            newChild.AppendChild(this.doc.CreateTextNode(p_strParaVal));
            documentElement.AppendChild(newChild);
            return newChild;
        }

        public XmlNode FindNodeByPath(string p_strPath)
        {
            XmlNode node = null;
            try
            {
                node = this.doc.DocumentElement.SelectSingleNode(p_strPath);
            }
            catch (Exception)
            {
            }
            return node;
        }

        public string FindTextByPath(string p_strPath)
        {
            string str = "";
            XmlNode node = null;
            try
            {
                XmlNode documentElement = this.doc.DocumentElement;
                if (documentElement.SelectSingleNode(p_strPath).ChildNodes.Count <= 0)
                {
                    return str;
                }
                if (documentElement.SelectSingleNode(p_strPath).ChildNodes[0] != null)
                {
                    node = documentElement.SelectSingleNode(p_strPath).ChildNodes[0];
                    str = node.Value;
                }
            }
            catch (Exception)
            {
            }
            return str;
        }

        public XmlDocument getRoot()
        {
            return this.doc;
        }

        public string GetXML()
        {
            return this.doc.OuterXml;
        }

        public bool OpenXml(string p_strFileName)
        {
            bool flag = false;
            try
            {
                this.doc.Load(p_strFileName);
                flag = true;
            }
            catch (Exception exception)
            {
                Console.Write(exception.Message);
            }
            return flag;
        }

        public bool SaveXml(string p_strFileName)
        {
            bool flag = false;
            try
            {
                this.doc.Save(p_strFileName);
                flag = true;
            }
            catch (Exception exception)
            {
                Console.Write(exception.Message);
            }
            return flag;
        }
    }
}
